using UnityEngine;
using System.Collections;




public class Player : MonoBehaviour {
	
	public int health = 100;
	public int healthMax = 100;
	
	public int rotateSpeed = 450;
	public float moveSpeed = 10.0f;
	
	public GameObject[] WEAPONS = new GameObject[4];
	
	bool mainWep = true;
	
	private GameObject mainWeapon, secWeapon;
	// The current x-z move speed
	
	public Vector3 moveDirection = Vector3.zero;

	private bool isMoving = false;
	
	private float magicNumberToLimitDiagonalSpeed = 0.7071f;
	CharacterController controller;
	
	public Texture2D healthBarFrame;
	public Texture2D healthBarFill;
	
	
	
	void Awake () 
	{	
		controller = gameObject.GetComponent<CharacterController>();		
	}
	
	void UpdateMovement ()
	{
	
		// v and h will always be in range -1..1
		float v = Input.GetAxisRaw("Vertical");
		float h = Input.GetAxisRaw("Horizontal");
		
		
		isMoving = (Mathf.Abs (h) > 0.1 || Mathf.Abs (v) > 0.1);
		
		if(isMoving)
		{
			moveDirection = new Vector3(h, 0.0f,v);
			moveDirection.Normalize();
			
			if(Mathf.Abs (h) > 0.1 && Mathf.Abs (v) > 0.1) // you are moving diagonally
				controller.Move(moveDirection * (moveSpeed*magicNumberToLimitDiagonalSpeed) * Time.deltaTime);
			else
				controller.Move(moveDirection * moveSpeed * Time.deltaTime);
			
		}
			
		
		RotatePlayer();
	
		
	}

	void RotatePlayer()
	{	
		
	//	public static Vector3 GetMouseWorldPosition(Camera cam, GameObject relativeTo)// float distanceFromCamera)
	//{
	//	float dist = Vector3.Distance(relativeTo.transform.position, cam.transform.position);
	//	Vector3 pos = cam.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, dist));
	//	return pos;
	//}
		
		 // Generate a plane that intersects the transform's position with an upwards normal.
    	Plane playerPlane = new Plane(Vector3.up, transform.position);
 
    	// Generate a ray from the cursor position
    	Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 		
		
    	// Determine the point where the cursor ray intersects the plane.
    	// This will be the point that the object must look towards to be looking at the mouse.
    	// Raycasting to a Plane object only gives us a distance, so we'll have to take the distance,
    	//   then find the point along that ray that meets that distance.  This will be the point
    	//   to look at.
    	float hitdist = 0.0f;
    	// If the ray is parallel to the plane, Raycast will return false.
    	if (playerPlane.Raycast (ray, out hitdist)) 
		{
			
        	// Get the point along the ray that hits the calculated distance.
        	Vector3 targetPoint = ray.GetPoint(hitdist);
 
        	// Determine the target rotation.  This is the rotation if the transform looks at the target point.
        	Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
 
        	// Smoothly rotate towards the target point.
        	transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
			//transform.LookAt(targetPoint);
		}
 
			
	}

	void OnTriggerStay(Collider other)
	{
		// check for input to see if they want to pick up the new wep
		if(Input.GetKeyDown(KeyCode.E))
		{
			if(other.tag == "ASSAULT")
			{
				// Change the weapon here
				//Instantiate(
				health -= 50;
			}
		}
	}
	
	
	
	// Update is called once per frame
	void Update () 
	{
		if(0 >= health)
			DestroyObject(gameObject);
		
		UpdateMovement();
	
		if(Input.GetButtonDown("Fire1"))
		{
			
		}
		
		if(Input.GetButtonDown ("LoseHealth"))
			TakeDamage (10);
		
	}
	
	void TakeDamage(int damage)
	{
		health -= damage;
		
		if(0 > health)
			health = 0;
	}
	
	void OnGUI()
	{
		DrawHealth((float)health/healthMax);
	}
	
	void DrawHealth(float percentage)
	{
		
		if(!healthBarFill || !healthBarFrame)
			return;

		// Temporarily change GUI color (to 50% alpha)
		GUI.color = new Color(1,1,1,0.5f);
		
		// Draw Healthbar Fill texture
		GUI.DrawTexture(new Rect(0, 0, 256 * percentage, 64), healthBarFill);
				
		// Change GUI color back to normal
		GUI.color = new Color(1,1,1,1);
		
		// Draw Healthbar Frame texture
		GUI.DrawTexture(new Rect(0, 0, 256, 64), healthBarFrame);
		
		// Center text alignment for labels 
		GUI.skin.label.alignment = TextAnchor.MiddleCenter;
		
		// Draw Text Label
		GUI.Label(new Rect(0, 0, 256, 64), "Health");
		
	}
}
	

